const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 0,
        title: "正在抢购",
        time: "7:00-12:00",
        currentCateInfo: "7:00-12:00",
        nextCateInfo: "7:00-12:00",
        active: true
      },
      {
        id: 1,
        title: "即将开抢",
        time: "12:00-24:00",
        currentCateInfo: "7:00-12:00",
        nextCateInfo: "7:00-12:00",
        active: false
      },
    ],
    fiveList: [{
        img: '../../images/tb134.png',
        title: '红包转盘'
      },
      {
        img: '../../images/tb138.png',
        title: '热销推荐'
      },
      {
        img: '../../images/tb137.png',
        title: '新品上线'
      },
      {
        img: '../../images/tb136.png',
        title: '在线客服'
      },
      {
        img: '../../images/tb135.png',
        title: '商品分类'
      },
    ],
    statusBarHeight: getApp().globalData.statusBarHeight,
    rels: '', //倒计时
    hours: '',
    minutes: '',
    seconds: '',
    status: 0,
    page: 1,
    content: "",
    thumbUrl: getApp().globalData.thumbUrl,
    type: 1,
    MoreRobList: [],
    currentCateInfo: {},
    nextCateInfo: {},
    //立即抢购
    active: false
  },
  send(e) {
    var that = this
    const {
      index
    } = e.detail
    const {
      tabs
    } = this.data
    console.log(index)
    that.data.type = index + 1
    tabs.forEach(v => v.id == index ? v.active = true : v.active = false)
    that.setData({
      tabs
    })
    that.getMoreRobList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },
  robGoodsDetail: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var uid = wx.getStorageSync('uid');
    if (uid) {
      wx.navigateTo({
        url: '../robGoodsDetails/robGoodsDetails?id=' + id + "&name=" + name,
      })
    } else {
      wx.navigateTo({
        url: '../authorization/authorization',
      })
    }

    console.log(e)
  },
  showFrame: function () {
    wx.showToast({
      title: '抢购暂未开始',
      icon: "none",
      duration: 2000
    })
  },
  /**
   * 生命周期函数
   */
  onReady: function () {

  },

  countDownCallbackFn: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    var param = {
      id: id
    };
    app.wxRequest('POST', "Index/checkGroupStatus", param, (res) => {
      var code = res.code;
      if (code == 1) {
        setTimeout(function () {
          that.onLoad();
        }, 1000)
      }
    }, (err) => {
      console.log(err.errMsg)
    })
    console.log(e)
  },
  //获取首页数据
  getGoodsIndex() {
    var that = this;
    var uid = wx.getStorageSync('uid');
    var param = {
      uid: uid,
    };
    // wx.setStorageSync('uid', 1);
    app.wxRequest('POST', "Index/getIndexData", param, (res) => {
      console.log(res)
      var data = res.data;
      that.setData({
        bannerList: data.bannerList,
        noticeList: data.noticeList,
        cateInfo: data.cateInfo,
        goodsRobList: data.goodsRobList,
        groupOrderList: data.groupOrderList,
        status: data.status,
        page: 1
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  //获取限时抢购数据
  getMoreRobList() {
    var that = this;
    var params = {
      type: that.data.type
    };
    // wx.setStorageSync('uid', 1);
    app.wxRequest('POST', "Index/getMoreRobList", params, (res) => {
      console.log(res)

      that.setData({
        MoreRobList: res.data.goodsRobList,
        currentCateInfo: res.data.currentCateInfo,
        nextCateInfo: res.data.nextCateInfo
      })

    }, (err) => {
      console.log(err.errMsg)
    })
  },

  //立即/等待抢购
  // buying() {
  //   var that = this
  //   var nowTime = new Date().getTime()
  //   console.log(nowTime)
  //   Date.parse(that.currentCateInfo.start_time)
  //   // if(that.currentCateInfo.){}
  // },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGoodsIndex();
    this.getMoreRobList();
  },

  onUnload: function () {
    var that = this;
    that.setData({
      clearTimer: true
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.onLoad();
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /*
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    //console.log(page)
    var newPage = parseInt(page) + 1;
    var param = {
      page: newPage
    };
    var groupOrderList = that.data.groupOrderList;
    app.wxRequest('POST', "Index/getMost", param, (res) => {
      var data = res.data;
      var code = res.code;
      if (code == 1) {
        that.setData({
          groupOrderList: groupOrderList.concat(data),
          page: newPage
        })
      } else {
        wx.showToast({
          title: '我是有底线的...',
          icon: "none",
          duration: 1500
        })
      }

    }, (err) => {
      console.log(err.errMsg)
    })
  },

  toclassify(e) {
    var index = e.currentTarget.dataset.index;
    if (index == 4) {
      var uid = wx.getStorageSync('uid');
      if (uid) {
        wx.switchTab({
          url: '/pages/classify/classify',
        })
      } else {
        wx.navigateTo({
          url: '/pages/authorization/authorization',
        })
      }

    } else if (index == 0) {
          var uid = wx.getStorageSync('uid');
      if (uid) {
        wx.navigateTo({
          url: '/pages/fission/fission',
        })
      } else {
        wx.navigateTo({
          url: '/pages/authorization/authorization',
        })
      }
    } else {
      wx.showToast({
        title: '功能暂未开放,敬请期待',
        icon: "none",
        duration: 1500
      })
    }
  },
  goodsDetail: function (e) {
    // console.log(e);return false;
    var id = e.currentTarget.dataset.id;
    var gid = e.currentTarget.dataset.gid;
    var name = e.currentTarget.dataset.name;
    var uid = wx.getStorageSync('uid');
    if (!uid) {
      wx.navigateTo({
        url: '../authorization/authorization',
      })
      return false;
    }
    wx.navigateTo({
      url: '../shopDetails/shopDetails?name=' + name + "&id=" + id + "&group_id=" + gid,
    })
    // console.log(e)
  },
  getSearchContent: function (e) {
    var that = this;
    var content = e.detail.value;
    that.setData({
      content: content
    })
  },
  search_btn: function () {
    var that = this;
    var content = that.data.content;
    if (content) {
      wx.navigateTo({
        url: '../searchRes/searchRes?content=' + content,
      })
    } else {
      wx.showToast({
        title: '请输入商品名称',
        icon: "none",
        duration: 1500
      })
    }
  },
  goodsList: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../robGoodsList/robGoodsList?id=' + id,
    })
  },
})